package com.test.action;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.test.exception.PasswordException;
import com.test.exception.UsernameException;

//ServletRequestAware
//implements ServletResponseAware
public class LoginAction extends ActionSupport  {

	private String username;
	private String password;
  
	//private HttpServletRequest request;
	//private HttpServletResponse response;

	public String getUsername() {
		return username;
	}

	// from functions ,not from attributes
	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String execute() throws Exception {
		
		if (!"hello".equals(this.getUsername()))
		{
			throw new UsernameException("username invalid");
		}
		else if (!"world".equals(this.getPassword())){
			throw new PasswordException("password invalid");
		}
		else {
			return "success";
		}
//		if("hello".equals(this.getUsername().trim()) && "world".equals( this.getPassword().trim()))
//		{
//			Map<String, Object> map= ActionContext.getContext().getSession();
//			map.put("user", "vaild");
//			
//		//ActionContext.getContext().put("quoo", "helloworld");
//	    //request.setAttribute("quoo", "helloworld");
//			HttpServletResponse response =ServletActionContext.getResponse();
//			Cookie cookie = new Cookie ("username",this.getUsername());
//		    cookie.setMaxAge(1000);
//			response.addCookie(cookie);
//			return "success";
//		}
//		else 
//		{
//			this.addFieldError("username", "username or password error!");
//			return "failer";
//		}
	}
	
	public String login() throws Exception {
		
		System.out.println("login invoked");
		if("hello".equals(this.getUsername().trim()) && "world".equals( this.getPassword().trim()))
		{
			Map<String, Object> map= ActionContext.getContext().getSession();
			map.put("user", "vaild");
			
		//ActionContext.getContext().put("quoo", "helloworld");
	    //request.setAttribute("quoo", "helloworld");
			HttpServletResponse response =ServletActionContext.getResponse();
			Cookie cookie = new Cookie ("username",this.getUsername());
		    cookie.setMaxAge(1000);
			response.addCookie(cookie);
			return "success";
		}
		else 
		{
			this.addFieldError("username", "username or password error!");
			return "failer";
		}
	}

//	@Override
//	public void validate() {
//		// TODO Auto-generated method stub
//		if ( null == this.getUsername()  || "".equals(this.getUsername().trim())) {
//			this.addFieldError("username", "username required");
//		}
//		if ( null ==this.getPassword()   || "".equals(this.getPassword().trim())) {
//			this.addFieldError("password", "password required");
//		}
//	}

//	public void setServletResponse(HttpServletResponse response)
//	{
//		
//		this.response =response;
//	}

//	public void setServletRequest(HttpServletRequest request) 
//	{
//		System.out.println("dependency injection");
//		this.request= request;
//	}
	
	
}